package com.yanqu.road.server.cmd.activity.doublespring;

import com.yanqu.road.entity.activity.doublespring.data.DoubleSpringUserData;
import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.DoubleSpringProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivity;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivityMgr;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

@Cmd(code = Protocol.C_CROSS_DOUBLE_SPRING_TRAVEL, desc = "踏春游记-出行")
public class CrossDoubleSpringTravelCmd extends CrossCommand {

    private long playerId;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        playerId = packet.getPlayerId();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        DoubleSpringProto.DoubleSpringServerToCrossTravelReqMsg reqMsg = DoubleSpringProto.DoubleSpringServerToCrossTravelReqMsg.parseFrom(packet.getBytes());
        CrossDoubleSpringActivity activity = CrossDoubleSpringActivityMgr.getActivity(reqMsg.getActivityId());
        if (activity == null) {
            ret = GameErrorCode.E_DOUBLE_SPRING_ACTIVITY_NOT_EXIST;
            return;
        }
        //存储userBase
        if(reqMsg.hasPlayerBaseData()){
            UserBaseInfo userBaseInfo = PlayerBasePb.parseToUserBaseInfo(reqMsg.getPlayerBaseData());
            CrossUserMgr.updateUserBaseInfo(packet.getPlayerId(), userBaseInfo);
        }

        //出行
        DoubleSpringUserData userData = activity.travel(reqMsg.getActivityId(), packet.getPlayerId(), serverId);
        DoubleSpringProto.DoubleSpringCrossTravelMsg.Builder builder = DoubleSpringProto.DoubleSpringCrossTravelMsg.newBuilder();
        builder.setRet(0);
        builder.setActivityId(reqMsg.getActivityId());
        builder.setScore(userData.getScore());
        builder.setTotalScore(userData.getTotalScore());
        builder.setPartnerUserId(userData.getPartnerUserId());
        builder.setAddScore(activity.getTravelScore());
        MessageHelper.sendPacket(serverId, packet.getPlayerId(), YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_TRAVEL_FROM_CROSS, builder));
    }

    @Override
    public void sendData() {
        if (ret != 0) {
            DoubleSpringProto.DoubleSpringCrossTravelMsg.Builder builder = DoubleSpringProto.DoubleSpringCrossTravelMsg.newBuilder();
            builder.setRet(ret);
            MessageHelper.sendPacket(serverId, playerId, YanQuMessageUtils.buildMessage(Protocol.S_DOUBLE_SPRING_TRAVEL_FROM_CROSS, builder));
        }
    }
}
